/*
*************************************************************************
*西瑞克斯研发管理系统
*版 本: 1.0   
*日 期：02/06/2012    
*作 者： 王浩 QQ：873987060   
*************************************************************************   
*/ 
package com.application.yfprojectsurvey.struts.actions;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.application.attachment.entity.Attachment;
import com.application.attachment.service.AttachmentService;
import com.application.attachment.struts.form.AttachmentForm;
import com.application.yfprojectsurvey.commons.Constants;
import com.application.yfprojectsurvey.entity.YfProjectSurvey;
import com.application.yfprojectsurvey.service.YfProjectSurveyService;
import com.application.yfprojectsurvey.struts.form.YfProjectSurveyForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.FileUtils;
import com.manager.commons.RandomKey;
import com.manager.commons.page.PageBean;
 /**
 * 
 * Description:YfProjectSurveyAction<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060    <br />
 * 
 * History: <br />
 * 
 * 02/06/2012 
 * 
 */
public class YfProjectSurveyAction extends DispatchAction
{
       
       /**
	 * Service层实例
	 */
	private YfProjectSurveyService yfProjectSurveyService;
	
	private AttachmentService attachmentService;

	/**
	 * 日志
	 */
	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * 实体对象
	 */
        private YfProjectSurvey yfProjectSurvey;


        /**
	 * 采用partialList方式分页程序
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward getYfProjectSurveyPartialList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		log.debug("searchYfProjectSurvey" + "开始");
		YfProjectSurveyForm yfProjectSurvey = (YfProjectSurveyForm)form;
		// 统计总记录数的sql语句
		int resultSize = 0;
		// 初始化pageBean供分页所用
		PageBean pageBean = null;
		// 取得当前页list数据
		List yfProjectSurveyList = null;
		resultSize = yfProjectSurveyService.getAllCountYfProjectSurvey(yfProjectSurvey);
		pageBean = PageBean.getPageBean("yfProjectSurveyTab", resultSize,request);
		yfProjectSurveyList = yfProjectSurveyService.searchYfProjectSurvey(yfProjectSurvey,pageBean);
		request.setAttribute("yfProjectSurveyList", yfProjectSurveyList);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		log.debug("searchYfProjectSurvey" + "结束");

		return mapping.findForward(Constants.SUCCESS);
	}

        /**
	 * 到达新增页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toAddYfProjectSurvey(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toAddYfProjectSurveys");
		YfProjectSurveyForm yfProjectSurveyForm = (YfProjectSurveyForm) form;
		yfProjectSurveyForm.setId(RandomKey.generateID_Long_19());
		yfProjectSurveyForm.setCreateDate(new Date());
		yfProjectSurveyForm.setRname(CommonUtil.getCurrentUser(request).getRname());
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 增加
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward addYfProjectSurvey(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("addYfProjectSurveys");
		 YfProjectSurvey yfProjectSurvey = this.getYfProjectSurvey(form);
		 yfProjectSurvey.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfProjectSurvey.setCreateDate(new Date());

		this.yfProjectSurveyService.insertObject(yfProjectSurvey);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 删除
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward deleteYfProjectSurvey(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("deleteYfProjectSurveys");
		String serverPath=CommonUtil.ATTACHE_FILE;
		if(StringUtils.isBlank(serverPath)){
			serverPath=request.getSession().getServletContext().getRealPath("/"); 
		}
		YfProjectSurveyForm yfProjectSurveyForm = (YfProjectSurveyForm) form;
		this.yfProjectSurveyService.deleteYfProjectSurveys(yfProjectSurveyForm);
		AttachmentForm attachmentForm = new AttachmentForm();
		attachmentForm.setFid(yfProjectSurveyForm.getId().toString());
		attachmentForm.setType(com.manager.commons.Constants.FJ_TYPE_XMDY);
		List<Attachment> attachment = attachmentService.queryAttachment(attachmentForm);
		if(attachment!=null && attachment.size()>0){
			for (Attachment attachment2 : attachment) {
				File file = new File(serverPath + attachment2.getUrl());
		        file.delete();
				attachmentService.deleteObject(attachment2);
				
			}
		}
		return mapping.findForward(Constants.SUCCESS);
	}

	/**
	 * 到达更新页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward toUpdateYfProjectSurvey(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("toUpdateYfProjectSurveys");
		YfProjectSurveyForm yfProjectSurveyForm = (YfProjectSurveyForm) form;
		yfProjectSurvey = this.yfProjectSurveyService.searchYfProjectSurvey(yfProjectSurveyForm).get(0);
		request.setAttribute(Constants.YFPROJECTSURVEY, yfProjectSurvey);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}

        
	/**
	 * 更新
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward updateYfProjectSurvey(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log.debug("updateYfProjectSurveys");
		YfProjectSurvey yfProjectSurvey = this.getYfProjectSurvey(form);
	        yfProjectSurvey.setOperateid(CommonUtil.getCurrentUser(request).getId());
		 yfProjectSurvey.setCreateDate(new Date());
		this.yfProjectSurveyService.updateObject(yfProjectSurvey);

		return mapping.findForward(Constants.SUCCESS);
	}
        
	/**
	 * 查看页面
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward viewYfProjectSurvey(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("viewYfProjectSurvey");

		YfProjectSurveyForm yfProjectSurveyForm = (YfProjectSurveyForm) form;
		yfProjectSurvey = this.yfProjectSurveyService.searchYfProjectSurvey(yfProjectSurveyForm).get(0);
		request.setAttribute(Constants.YFPROJECTSURVEY, yfProjectSurvey);
		request.setAttribute("orgSelect", CommonUtil.getOrgSelectNoZB());
		return mapping.findForward(Constants.SUCCESS);
	}
	/**
	 * 下载附件
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward downAttachmentZip(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		log.debug("downAttachmentZip" + "开始");
		YfProjectSurveyForm yfProjectSurveyForm = (YfProjectSurveyForm) form;
		AttachmentForm attachmentForm = new AttachmentForm();
		attachmentForm.setFid(yfProjectSurveyForm.getFid());
		attachmentForm.setType(com.manager.commons.Constants.FJ_TYPE_XMDY);
		List attacheList = attachmentService.queryAttachment(attachmentForm);
		List<File> fileList = new ArrayList<File>();
		String serverPath=CommonUtil.ATTACHE_FILE;
		if(StringUtils.isBlank(serverPath)){
			serverPath=request.getSession().getServletContext().getRealPath("/"); 
		}
		if(attacheList!=null && attacheList.size()>0){
			for (Object object : attacheList) {
				Attachment attchement = (Attachment) object;
				File file = new File(serverPath+attchement.getUrl());
				if(file.exists()){
					fileList.add(file);
				}
			}
		}
		try
		{
			FileUtils.downloadZIP(fileList,serverPath,yfProjectSurveyForm.getFileName()+".rar", response);
		}
		catch(Exception e)
		{
			log.error("下载附件失败！", e);
		}
		return null;
	} 

	// get--set
	/**
	 * @return the YfProjectSurveyService
	 */
	public YfProjectSurveyService getYfProjectSurveyService() {
		return this.yfProjectSurveyService;
	}

	/**
	 * @param YfProjectSurveyService
	 *  the YfProjectSurveyService to set
	 */
	public void setYfProjectSurveyService(YfProjectSurveyService yfProjectSurveyService) {
		this.yfProjectSurveyService = yfProjectSurveyService;
	}

	/**
	 * 获取YfProjectSurvey实体
	 * @param ActionForm
	 * @throws InvocationTargetException 
	 * @throws IllegalAccessException 
	 * @return the YfProjectSurvey
	 */
	public YfProjectSurvey getYfProjectSurvey(ActionForm form) throws IllegalAccessException, InvocationTargetException {
		YfProjectSurveyForm yfProjectSurveyForm = (YfProjectSurveyForm) form;
		yfProjectSurvey = new YfProjectSurvey();
		BeanUtils.copyProperties(yfProjectSurvey, yfProjectSurveyForm);
		return yfProjectSurvey;
	}
    
        public void setYfProjectSurvey(YfProjectSurvey yfProjectSurvey) {
		this.yfProjectSurvey = yfProjectSurvey;
	}

		public void setAttachmentService(AttachmentService attachmentService) {
			this.attachmentService = attachmentService;
		}

}
